/*
 * @Author: your name
 * @Date: 2021-11-29 22:01:46
 * @LastEditTime: 2021-11-29 22:01:46
 * @LastEditors: your name
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \li-yuwei\client\src\utils\handlerDate.js
 */
const handlerDate = (data) => {
    let result = [];
    data.forEach((item, index) => {
        data.forEach((val, idx) => {
            if (index !== idx && item.year === val.year) {
                //判断是否存在
                const index = result.findIndex((v) => v.id === item.id);
                //不存在
                if (index === -1) {
                    //去重
                    const idx = result.findIndex((every) => every.year === item.year);
                    if (idx === -1) {
                        result.push({ year: item.year, data: [item] });
                    } else {
                        //去重
                        const int = result[idx].data.findIndex((i) => i.id === item.id);
                        if (int === -1) {
                            result[idx].data.push(item);
                        }
                    }
                }
            } else {
                const idx = result.findIndex((ev) => ev.year === item.year);
                if (idx === -1) {
                    result.push({ year: item.year, data: [item] });
                }
            }
        });
    });
    //重复执行操作
    result.forEach((item, index) => {
        let monthList = [];
        item.data.forEach((val, idx) => {
            item.data.forEach((v, i) => {
                if (idx !== i && val.month === v.month) {
                    const index = monthList.findIndex((i) => i.id === val.id);
                    if (index === -1) {
                        const idx = monthList.findIndex(
                            (every) => every.month === val.month
                        );
                        if (idx === -1) {
                            monthList.push({ month: val.month, data: [val] });
                        } else {
                            const int = monthList[idx].data.findIndex((i) => i.id === val.id);
                            if (int === -1) {
                                monthList[idx].data.push(val);
                            }
                        }
                    }
                } else {
                    const idx = monthList.findIndex((every) => every.month === val.month);
                    if (idx === -1) {
                        monthList.push({ month: val.month, data: [val] });
                    }
                }
            });
        });
        result[index].monthList = monthList;
    });
    result.forEach((item) => {
        delete item.data;
    });
    return result;
};
export default handlerDate;